package service

import (
	"context"
	"github.com/jinzhu/gorm"
	"nearby/biz/common"
	"nearby/biz/dal/db/po"
	"nearby/biz/dal/db/repo"
	"nearby/biz/model"
)

type DeleteUserService struct {
}

func (ss DeleteUserService) Execute(ctx context.Context, req *model.DeleteUserRequest) (*model.DeleteUserResponse, error) {
	userRepo := repo.NewUserRepo(ctx)
	err := userRepo.Do(ctx, func(sql *gorm.DB) error {
		if err := sql.Table("user_center").Where("user_id = ?", req.UserID).Delete(&po.User{}).Error; err != nil {
			return err
		}
		if err := sql.Table("user_account").Where("user_id = ?", req.UserID).Delete(&po.UserAccount{}).Error; err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		return nil, common.NewBizErr(common.BizErrCode, err.Error(), err)
	}
	return &model.DeleteUserResponse{
		Meta: common.MetaOk,
	}, nil
}
